package com.leo.base_project.Utils;

import android.app.Activity;
import android.content.Context;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

import static com.leo.base_project.Utils.UIUtils.toast;

/**
 * @program: BaseProject
 * @description: 系统相关工具
 * @author: Leo
 * @create: 2021-03-28 11:05
 **/
public class SystemUtils {
    /* @Description: 获取权限
     * @Param: activity 对应activity的this
     * @return: null
     * @Author: Leo
     * @Date: 2021/3/28
     **/
    public static void getPermissions(Activity activity){
        XXPermissions.with(activity)
//                 这两个权限不能动态请求，请求MANAGE_EXTERNAL_STORAGE 即可
//                .permission(Permission.READ_EXTERNAL_STORAGE)
//                .permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .permission(Permission.READ_PHONE_STATE)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            toast("获取存储权限成功");
                        } else {
                            toast("获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予录音和日历权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(activity, permissions);
                        } else {
                            toast("获取存储权限失败");
                        }
                    }
                });
    }


}
